﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SFML.Graphics;
using Theta.Animations;

namespace Theta.Animations
{
    public class AnimatedSprite : Sprite, IUpdateable
    {
        bool _playing;

        //Hiding Image, you should use SpriteSheet(Yea, doesn't matter to be honest)
        private new Image Image { get; set; }

        public Image SpriteSheet
        {
            get { return Image; }
            set { Image = value; }
        }

        public Animation Animation { get; set; }
        public Animation DefaultAnimation { get; set; }

        public AnimatedSprite() : base()
        {
        }
        public AnimatedSprite(Image spriteSheet) : base(spriteSheet)
        {
        }

        public void Draw(RenderWindow renderWindow)
        {
            renderWindow.Draw(this);
        }

        public void PauseAnimation()
        {
            _playing = false;
        }

        public void StopAnimation()
        {
            //Note: Animation should handle stoping / resuming
            _playing = false;
            Animation.CurIndex = 0;
        }

        public void PlayAnimation()
        {
            _playing = true;
        }

        public void PlayAnimation(Animation anim)
        {
            PlayAnimation(anim, 0);
        }
        
        public void PlayAnimation(Animation anim, int startFrame)
        {
            Animation = anim.Copy();
            Animation.CurIndex = startFrame;
            _playing = true;
        }

        public void Update(TimeSpan elapsedTime)
        {
            //Null check will be evaluated first
            if(Animation == null || Animation.Ended)
            {
                if (DefaultAnimation != null)
                {
                    DefaultAnimation.Update(elapsedTime);
                    SubRect = DefaultAnimation.GetRect();
                }
            }
            else if(_playing)
            {
                Animation.Update(elapsedTime);
                SubRect = Animation.GetRect();
            }
        }
    }
}
